---
title: "Supplemental Info (CPS R&R)"
---

# Setup

```{r packages and themes}
# SETUP 
~
knitr::opts_chunk$set(include = F)

## Packages 
library(tidyverse)
library(conflicted)
library(here)
library(performance)
library(marginaleffects)
library(haven)
library(texreg)
library(glmmTMB)
library(xtable)
library(parallel)
library(ggpubr)

conflict_prefer("select", "dplyr")
conflict_prefer("filter", "dplyr")

# Make things faster 
cores <-  detectCores() - 2

## Plot theme 

brb_theme <- function() {
  theme_pubclean() %+replace%
    theme(
      axis.text.x = element_text(color = "black", size = 11),
      axis.text.y = element_text(color = "black", size = 11, hjust = .95),
      axis.title.y = element_text(color = "black", angle = 90),
      plot.title = element_text(hjust = 0.5)
    )
}
```

# Load and Clean 

```{r select data and make factors}

## Load Data 

load(here("data/lapop_10_18.Rdata")) # slim data -- recodes only

# DATA PREP 


dichot.vars.cong <- c("close.cong", "rural", "female", "non_voter", "winner", "year")
dichot.vars.court <- c("close.court", "rural", "female", "non_voter", "winner", "year")


# Select vars and make factors
cong_dat <- lapop.cleaned %>%
  mutate(across(all_of(dichot.vars.cong), factor)) %>% 
  zap_formats() %>% 
  zap_labels()


court_dat <- lapop.cleaned %>%
  mutate(across(all_of(dichot.vars.court), factor)) %>% 
  zap_formats() %>% 
  zap_labels()


```

# Descriptives

```{r descriptives}

# Country year correlations
corr_dat <- cong_dat %>% 
  select(country, year, maj, pop.speech, dem.best.tj, mass.polr, v2x_libdem, v2x_corr) %>% 
  unique() %>% 
  na.omit()

corrs <- round(cor(corr_dat[3:8]),2)  
corrs[lower.tri(corrs)] <- ""
xtable(corrs)

```

# Fig: Populist Speech by Wave

```{r make pop speech by wave fig, warning=F}

# 1 - FIG - POPULIST SPEECH BY WAVE

#Pres populism
pres_populism <- lapop.cleaned %>% 
  select(iso3c, year.fac, wave, country,  pop.speech) %>%
  unique.data.frame() %>% 
  arrange(pop.speech)


ggplot(pres_populism, aes(wave, pop.speech)) + 
  geom_dotplot(binaxis = "y",dotsize = 1, stackdir = "center") + 
  labs(x= "Survey Wave", 
       y = "Populist Discourse", 
       title = "") +
  brb_theme()

# save 
ggsave(here("figures/si_pop_by_wave.pdf"), height = 6, width = 8)


```


# Models w/ Corruption


```{r close congress models with corruption}

## No interaction 

cong_mod1c <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + winner + non_voter + pop.speech +
                      maj + v2x_corr + mass.polr + dem.best.tj + (1 | country:year) + 
                      (winner + non_voter | country) + year.fac")


cong_fit1c <- glmmTMB(
  formula = cong_mod1c,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))



## Supporter x Populist Speech

cong_mod2c <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter + pop.speech * winner +
                      maj + v2x_corr +  mass.polr + dem.best.tj + (1 | country:year) +
                      (winner + non_voter | country) + year.fac")


cong_fit2c <- glmmTMB(
  formula = cong_mod2c,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))



## Supporter x Pop Speech + Non-Voter x Pop Speech 

cong_mod3c <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      maj + v2x_corr + mass.polr + dem.best.tj + (1 | country:year) +   
                      (winner + non_voter | country) + year.fac")


cong_fit3c <- glmmTMB(
  formula = cong_mod3c,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Save tables
coef_labs_corr <- list("pop.speech" = "Populist Discourse", "pop.speech:winner1" = "Populist Speech x Pres. Supporter",
             "pop.speech:non_voter1" = "Populist Speech x Non-Voter", "winner1" = "Presidential Supporter", 
             "non_voter1" = "Non_Voter","dem.best" = "Support for Democracy", "sat_dem" = "Satisfaction with Democracy", 
             "edu" = "Education", "wealth" = "Wealth", "rural1" = "Rural", "female1" = "Female", 
             "trust.people" = "Interpersonal Trust", "neigh.safe" = "Neighborhood Safe", 
             "econ.eval" = "Economic Evaluation", "maj" = "Legislative Control", 
             "v2x_corr" = "Government Corruption", "mass.polr" = "Mass Polarization", 
             "dem.best.tj" = "Mass Support for Democracy")

cong_mods1c <- list(cong_fit1c, cong_fit2c, cong_fit3c)
screenreg(cong_mods1c, file = here("tables/cong_mods_corr.txt"))

screenreg(cong_mods1c,  stars = c(.1, .05, .01))

texreg(cong_mods1c, caption.above = T, 
       caption = "Models of support for closing congress. Models include government corruption rather than the liberal democracy index.",
       custom.coef.map = coef_labs_corr, 
       stars = c(.1, .05, .01),
       file = here("tables/cong_mods_corr.tex"))

```


```{r close court models with corruption}

## No interaction 

court_mod1c <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + pop.speech + winner + non_voter +
                        maj + v2x_corr + mass.polr + dem.best.tj + (1 | country:year) +   
                        (winner + non_voter | country) + year.fac")

court_fit1c <- glmmTMB(
  formula = court_mod1c,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

## Populism x Presidential Supporter 

court_mod2c <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + non_voter + pop.speech * winner +
                         maj + v2x_corr +  mass.polr + dem.best.tj + (1 | country:year) +   
                        (winner + non_voter | country) + year.fac")

court_fit2c <- glmmTMB(
  formula = court_mod2c,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


##  Supporter x Pop Speech + Non-Voter x Pop Speech  

court_mod3c <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + pop.speech * winner + pop.speech * non_voter +
                         maj + v2x_corr + mass.polr + dem.best.tj + (1 | country:year) +    
                        (winner + non_voter | country) + year.fac")

court_fit3c <- glmmTMB(
  formula = court_mod3c,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


# Save to tables 

court_mods1c <- list(court_fit1c, court_fit2c, court_fit3c)

screenreg(court_mods1c)

screenreg(court_mods1c, file = here("tables/court_mods_corr.txt"))

texreg(court_mods1c, custom.coef.map = coef_labs_corr,
       caption.above = T,
       caption = "Models of support for closing the supreme court. Models include government corruption rather than the liberal democracy index.", 
       stars = c(.1, .05, .01),
       file = here("tables/court_mods_corr.tex"))


```

# Models w/ Party ID 

```{r close congres models with pid}

## Co-Partisan x Pop Speech  
cong_mod_pid1 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_partisan + pop.speech * pid_winner +
                      maj + v2x_libdem +  mass.polr + dem.best.tj + 
                      (1 | country:year) + (pid_winner + non_partisan | country) + year.fac")

cong_fit_pid1 <- glmmTMB(
  formula = cong_mod_pid1,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

##  Supporter x Pop Speech + Non-Voter x Pop Speech  
cong_mod_pid2 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_partisan * pop.speech + pop.speech * pid_winner +
                      maj + v2x_libdem +  mass.polr + dem.best.tj +
                      (1 | country:year) + (pid_winner + non_partisan | country) + year.fac")

cong_fit_pid2 <- glmmTMB(
  formula = cong_mod_pid2,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


```


```{r close court models with pid}

## Co-Partisan x Pop Speech  
court_mod_pid1 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + non_partisan + pop.speech * pid_winner +
                        maj + v2x_libdem + mass.polr + dem.best.tj +
                        (1 | country:year) + (pid_winner + non_partisan | country) + year.fac")

court_fit_pid1 <- glmmTMB(
  formula = court_mod_pid1,
    control=glmmTMBControl(optimizer=optim,
               optArgs=list(method="L-BFGS-B"),
               parallel = cores),
  data = court_dat,
  family = binomial(link = "logit"))



##  Supporter x Pop Speech + Non-Voter x Pop Speech  
court_mod_pid2 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + non_partisan * pop.speech +  pid_winner * pop.speech +
                        maj + v2x_libdem + mass.polr + dem.best.tj +
                        (1 | country:year) + (pid_winner + non_partisan | country) + year.fac")

court_fit_pid2 <- glmmTMB(
  formula = court_mod_pid2,
    control=glmmTMBControl(optimizer=optim,
               optArgs=list(method="L-BFGS-B"),
               parallel = cores),
  data = court_dat,
  family = binomial(link = "logit"))

```


```{r pid models to latex}

screenreg(list(cong_fit_pid1, cong_fit_pid2, court_fit_pid1, court_fit_pid2),
          file = here("tables/supp_pid_mods.txt"))

screenreg(list(cong_fit_pid1, cong_fit_pid2, court_fit_pid1, court_fit_pid2),
          stars = c(.1, .05, .01))


coef_labs_pid <- list("pop.speech" = "Populist Discourse", "pop.speech:pid_winner" = "Populist Speech x Incumbent Partisan",
             "non_partisan:pop.speech" = "Populist Speech x Non-Partisan", "winner1" = "Presidential Supporter", 
             "non_voter1" = "Non_Voter","dem.best" = "Support for Democracy", "sat_dem" = "Satisfaction with Democracy", 
             "edu" = "Education", "wealth" = "Wealth", "rural1" = "Rural", "female1" = "Female", 
             "trust.people" = "Interpersonal Trust", "neigh.safe" = "Neighborhood Safe", 
             "econ.eval" = "Economic Evaluation", "maj" = "Legislative Control", 
             "v2x_corr" = "Government Corruption", "mass.polr" = "Mass Polarization", 
             "dem.best.tj" = "Mass Support for Democracy")

capt <- "Models of support for closing congress or the court. Support for the incumbent is measured as identification with the incumbent president’s party"

texreg(list(cong_fit_pid1, cong_fit_pid2, court_fit_pid1, court_fit_pid2),
       custom.coef.map = coef_labs_pid, 
       caption.above = T,
       caption = capt, 
       stars = c(.1, .05, .01),
       label = "tab:si_models_pid",
       file = here("tables/supp_pid_mods.tex"))

```


# Models w/ V-Dem Polar


```{r congress models with vdem polar}

## No interaction 
cong_mod1a <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter + winner + pop.speech +
                      maj + v2x_libdem +  vdem.polar + dem.best.tj +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")


cong_fit1a <- glmmTMB(
  formula = cong_mod1a,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

## Supporter x Populist Speech 
cong_mod2a <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter + pop.speech * winner +
                      maj + v2x_libdem +  vdem.polar + dem.best.tj +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

cong_fit2a <- glmmTMB(
  formula = cong_mod2a,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

## Supporter x Pop Speech + Non-Voter x Pop Speech 
cong_mod3a <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter * pop.speech + pop.speech *winner +
                      maj + v2x_libdem +  vdem.polar + dem.best.tj +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")


cong_fit3a <- glmmTMB(
  formula = cong_mod3a,
  data = cong_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Tables
alt_cong_mods1 <- list(cong_fit1a, cong_fit2a, cong_fit3a)
screenreg(alt_cong_mods1, file = here("tables/cong_models_altpolar.txt"))

coef_labs_polar_si <- list("pop.speech" = "Populist Discourse", 
                           "pop.speech:winner1" = "Populist Speech $\\times$ Pres. Supporter",
                           "non_voter1:pop.speech" = "Populist Speech $\\times$ Non-Voter",
                           "winner1" = "Presidential Supporter", "non_voter1" = "Non_Voter",
                           "dem.best" = "Support for Democracy", "sat_dem" = "Satisfaction with Democracy", 
                           "edu" = "Education", "wealth" = "Wealth", "rural1" = "Rural", "female1" = "Female", 
                           "trust.people" = "Interpersonal Trust", "neigh.safe" = "Neighborhood Safe", 
                           "econ.eval" = "Economic Evaluation", "maj" = "Legislative Control", 
                           "v2x_libdem" = "Liberal Democracy Index", "vdem.polar" = "Polarization (V-Dem)", 
                           "dem.best.tj" = "Mass Support for Democracy")


texreg(alt_cong_mods1,
       custom.coef.map = coef_labs_polar_si,
       caption = "Models of support for closing the congress. The polarization measure is from the V-Dem Dataset.",
       label = "tab:cong_mods_altpolar",
       caption.above = T,
       stars = c(0.1, 0.05, 0.01, 0.15),
       float.pos = "H",
       file = here("tables/cong_models_altpolar.tex"))

screenreg(alt_cong_mods1)

```


```{r court models with vdem polar}

## No interaction 
court_mod1a <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter + winner + pop.speech +
                      maj + v2x_libdem +  vdem.polar + dem.best.tj +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")


court_fit1a <- glmmTMB(
  formula = court_mod1a,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

## Supporter x Populist Speech
court_mod2a <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter + pop.speech * winner +
                      maj + v2x_libdem +  vdem.polar + dem.best.tj +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit2a <- glmmTMB(
  formula = court_mod2a,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

## Supporter x Pop Speech + Non-Voter x Pop Speech 
court_mod3a <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter * pop.speech + pop.speech * winner +
                      maj + v2x_libdem +  vdem.polar + dem.best.tj +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")


court_fit3a <- glmmTMB(
  formula = court_mod3a,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Tables
alt_court_mods1 <- list(court_fit1a, court_fit2a, court_fit3a)
screenreg(alt_court_mods1, file = here("tables/court_models_altpolar.txt"))
screenreg(alt_court_mods1)

texreg(alt_court_mods1, 
       custom.coef.map = coef_labs_polar_si,
       file = here("tables/court_models_altpolar.tex"),
       label = "tab:court_mods_alt_polar",
       caption.above = T,
       stars = c(0.1, 0.05, 0.01),
       caption = "Models of support for closing the court. The polarization measure is from the V-Dem Dataset.",
       float.pos = "H"
       )

```


# Models w/ Only Vars of Interest

```{r slim congress models}


cong_mod_slim1 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + winner + non_voter + pop.speech +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")



cong_fit_slim1 <- glmmTMB(
  formula = cong_mod_slim1,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

cong_mod_slim2 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval + non_voter + pop.speech * winner +
                      (1 | country:year) + (winner + non_voter | country) + year")


cong_fit_slim2 <- glmmTMB(
  formula = cong_mod_slim2,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


cong_mod_slim3 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

cong_fit_slim3 <- glmmTMB(
  formula = cong_mod_slim3,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Tables

coef_labs_slim <- list("pop.speech" = "Populist Discourse", "pop.speech:winner1" = "Populist Speech x Pres. Supporter",
             "pop.speech:non_voter1" = "Populist Speech x Non-Voter", "winner1" = "Presidential Supporter", 
             "non_voter1" = "Non_Voter","dem.best" = "Support for Democracy", "sat_dem" = "Satisfaction with Democracy", 
             "edu" = "Education", "wealth" = "Wealth", "rural1" = "Rural", "female1" = "Female", 
             "trust.people" = "Interpersonal Trust", "neigh.safe" = "Neighborhood Safe", 
             "econ.eval" = "Economic Evaluation", "maj" = "Legislative Control")

screenreg(list(cong_fit_slim1, cong_fit_slim2, cong_fit_slim3), 
          file = here("tables/cong_models_slim.txt"))

screenreg(list(cong_fit_slim1, cong_fit_slim2, cong_fit_slim3))

texreg(list(cong_fit_slim1, cong_fit_slim2, cong_fit_slim3),
       custom.coef.map = coef_labs_slim,
       label = "tab:si_cong_mods_slim",
       caption = "Models of support for closing congress. Models include only the individual-level variables, populist discourse, and the main interaction terms.",
       caption.above = T, 
       float.pos = "H",
       stars = c(.1, .05, .01),
       file = here("tables/cong_models_slim.tex"))


```


```{r slim court models}

court_mod_slim1 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + pop.speech + winner + non_voter +
                        (1 | country:year) +  (winner + non_voter | country) + year.fac")

court_fit_slim1 <- glmmTMB(
  formula = court_mod_slim1,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

court_mod_slim2 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + non_voter + pop.speech * winner +
                        (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit_slim2 <- glmmTMB(
  formula = court_mod_slim2,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


court_mod_slim3 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + pop.speech * winner + pop.speech * non_voter +
                        (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit_slim3 <- glmmTMB(
  formula = court_mod_slim3,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

#Tables 

screenreg(list(court_fit_slim1, court_fit_slim2, court_fit_slim3), 
          file = here("tables/court_models_slim.txt"))


screenreg(list(court_fit_slim1, court_fit_slim2, court_fit_slim3))
texreg(list(court_fit_slim1, court_fit_slim2, court_fit_slim3), 
       file = here("tables/court_models_slim.tex"),
       custom.coef.map = coef_labs_slim,
       label = "tab:si_court_mods_slim",
       caption = "Models of support for closing  the court. Models include only the individual-level variables, populist discourse, and the main interaction terms.",
       caption.above = T,
       stars = c(.1, .05, .01),
       float.pos = "H"
       )

```

# Models w/ Additional Cross-Level Interactions 


```{r close congress cross-level interactions}
# Close Congress Models ----------------------#

# Interaction with Legislative Control
cong_mod_inter1 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      maj * winner + maj * non_voter + 
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

cong_fit_inter1 <- glmmTMB(
  formula = cong_mod_inter1,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


# Interaction with liberal democracy
cong_mod_inter2 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      v2x_libdem * winner + v2x_libdem * non_voter + 
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

cong_fit_inter2 <- glmmTMB(
  formula = cong_mod_inter2,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


# Interaction with polarization
cong_mod_inter3 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      mass.polr * winner + mass.polr * non_voter + 
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

cong_fit_inter3 <- glmmTMB(
  formula = cong_mod_inter3,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Interaction with Mass support for Dem
cong_mod_inter4 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      dem.best.tj * winner + dem.best.tj * non_voter +  
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

cong_fit_inter4 <- glmmTMB(
  formula = cong_mod_inter4,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Interaction with corruption
cong_mod_inter5 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                       v2x_corr * winner + v2x_corr * non_voter + 
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

cong_fit_inter5 <- glmmTMB(
  formula = cong_mod_inter5,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))



#Tables

cong_inter_mods <-  list(cong_fit_inter1, cong_fit_inter2, 
                         cong_fit_inter3, cong_fit_inter4, 
                         cong_fit_inter5)


coef_labs_inter <- list("pop.speech" = "Populist Discourse", "pop.speech:winner1" = "Populist Speech x Pres. Supporter",
             "pop.speech:non_voter1" = "Populist Speech x Non-Voter", "winner1" = "Presidential Supporter", 
             "non_voter1" = "Non_Voter", "maj" = "Legislative Control", "winner1:maj" = "Maj x Pres Supporter",
             "non_voter1:maj" = " Maj x Non_voter", "v2x_libdem" = "Liberal Democracy Index",
             "winner1:v2x_libdem" = "Lib x Pres Supporter", "non_voter1:v2x_libdem" = "Lib x Non-Voter", 
             "mass.polr" = "Mass Polarization", "winner1:mass.polr" = " POLR x Pres Supporter",
             "non_voter1:mass.polr"  = " POLR x Non-Voter", "dem.best.tj" = "Mass Support for Democracy", 
             "winner1:dem.best.tj" = "MSD x Pres Supporter",  "non_voter1:dem.best.tj" = " MSD x Non-Voter",
             "v2x_corr" = "Government Corruption", "winner1:v2x_corr" = "Corr x Presidential Supporter",  
             "non_voter1:v2x_corr" = "Corr x Non-Voter")

screenreg(cong_inter_mods,
          custom.coef.map = coef_labs_inter,
          file = here("tables/cong_inter_mods.txt"))

screenreg(cong_inter_mods,
          custom.coef.map = coef_labs_inter,
             stars = c(.1, .05, .01))


texreg(cong_inter_mods,
       custom.coef.map = coef_labs_inter,
       label = "tab:si_cong_mods_inter",
       caption = "Models of support for closing congress. Models introduce interactions between presidential supporter, non-voter, and country-year-level variables",
       caption.above = T, 
       float.pos = "H",
       stars = c(.1, .05, .01),
       file = here("tables/cong_inter_mods.tex"))
```



```{r close court cross-level interactions}

# Close Court Models ----------------------#

# Interaction with Legislative Control
court_mod_inter1 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      maj * winner + maj * non_voter +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit_inter1 <- glmmTMB(
  formula = court_mod_inter1,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))


# Interaction with liberal democracy
court_mod_inter2 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      v2x_libdem * winner + v2x_libdem * non_voter +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit_inter2 <- glmmTMB(
  formula = court_mod_inter2,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Interaction with polarization
court_mod_inter3 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      mass.polr * winner + mass.polr * non_voter +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit_inter3 <- glmmTMB(
  formula = court_mod_inter3,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Interaction with Mass support for Dem
court_mod_inter4 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      dem.best.tj * winner + dem.best.tj * non_voter +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit_inter4 <- glmmTMB(
  formula = court_mod_inter4,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))

# Interaction with corruption
court_mod_inter5 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                       v2x_corr * winner + v2x_corr * non_voter +
                      (1 | country:year) + (winner + non_voter | country) + year.fac")

court_fit_inter5 <- glmmTMB(
  formula = court_mod_inter5,
  data = court_dat,
  control=glmmTMBControl(parallel = cores),
  family = binomial(link = "logit"))



#Tables
court_inter_mods <-  list(court_fit_inter1, court_fit_inter2, 
                         court_fit_inter3, court_fit_inter4, 
                         court_fit_inter5)


screenreg(court_inter_mods,
          custom.coef.map = coef_labs_inter,
          file = here("tables/court_inter_mods.txt"))

texreg(court_inter_mods,
       custom.coef.map = coef_labs_inter,
       label = "tab:si_court_mods_inter",
       caption = "Models of support for closing courtress. Models introduce interactions between presidential supporter, non-voter, and country-year-level variables",
       caption.above = T, 
       float.pos = "H",
       stars = c(.1, .05, .01),
       file = here("tables/court_inter_mods.tex"))

```

# Include individual-level populism

```{r models with individual-level pop}

# Close Congress

cong_mod_pop1 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + non_voter + 
                      pop_ind + maj + v2x_libdem + mass.polr + dem.best.tj + (1 | country:year) +
                      (winner + non_voter | country) + year")


cong_fit_pop1 <- glmmTMB(formula = cong_mod_pop1,
                     family = binomial(link = "logit"), 
                     data = cong_dat, 
                     control=glmmTMBControl(parallel = cores))

cong_mod_pop2 <- as.formula("close.cong ~ dem.best + sat_dem + edu + wealth + rural + wealth + female + 
                      trust.people + neigh.safe + econ.eval +  pop.speech * winner + pop.speech * non_voter + 
                      pop_ind + maj + v2x_libdem + mass.polr + dem.best.tj + (1 | country:year) +
                      (winner + non_voter | country) + year")


cong_fit_pop2 <- glmmTMB(formula = cong_mod_pop2,
                     family = binomial(link = "logit"), 
                     data = cong_dat, 
                     control=glmmTMBControl(parallel = cores))

# Close Court

court_mod_pop1 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + pop.speech * winner + non_voter +
                        pop_ind + maj + v2x_libdem + mass.polr + dem.best.tj + (1 | country:year) +
                        (winner + non_voter | country) + year")

court_fit_pop1 <- glmmTMB(
  formula = court_mod_pop1,
  data = court_dat,
  control=glmmTMBControl(optimizer=optim,
                         parallel = cores,
                         optArgs=list(method="BFG")),
  family = binomial(link = "logit"))


court_mod_pop2 <- as.formula("close.court ~ dem.best + sat_dem + edu + wealth + rural + wealth + female +
                        trust.people + neigh.safe + econ.eval + pop.speech * winner +  pop.speech * non_voter +
                        pop_ind + maj + v2x_libdem + mass.polr + dem.best.tj + (1 | country:year) +
                        (winner + non_voter | country) + year")

court_fit_pop2 <- glmmTMB(
  formula = court_mod_pop2,
  data = court_dat,
  control=glmmTMBControl(optimizer=optim,
                         parallel = cores,
                         optArgs=list(method="BFG")),
  family = binomial(link = "logit"))

# Tables

pop_mods <- list(cong_fit_pop1, cong_fit_pop2, court_fit_pop1, court_fit_pop2)


ind_pop_labs <- list("pop.speech" = "Populist Discourse", "pop.speech:winner1" = "Populist Speech $\\times$ Pres. Supporter",
             "pop.speech:non_voter1" = "Populist Speech $\\times$ Non-Voter", "winner1" = "Presidential Supporter", 
             "pop_ind" = "Populist Predisposition", "non_voter1" = "Non_Voter",
             "dem.best" = "Support for Democracy", "sat_dem" = "Satisfaction with Democracy", 
             "edu" = "Education", "wealth" = "Wealth", "rural1" = "Rural", "female1" = "Female", 
             "trust.people" = "Interpersonal Trust", "neigh.safe" = "Neighborhood Safe", 
             "econ.eval" = "Economic Evaluation", "maj" = "Legislative Control", 
             "v2x_libdem" = "Liberal Democracy Index", "mass.polr" = "Mass Polarization", 
             "dem.best.tj" = "Mass Support for Democracy")

screenreg(pop_mods,
          stars = c(.15, .1, .05, .01))

screenreg(pop_mods, 
          file = here("tables/mods_pop_ind.txt"))

texreg(pop_mods,
       file = here("tables/mods_pop_ind.tex"),
       custom.coef.map = ind_pop_labs,
       label = "tab:si_ind_pop",
       caption = "Models of support for closing  the court. Models include control a
       populist attitudes",
       caption.above = T,
       stars = c(.15, .1, .05, .01),
       float.pos = "H"
       )


```









